#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

namespace fastio{
    inline int in(){
        int x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
        return x*f;
    }
    int stk[30],tp;
    inline void out(int x){
        if(x<0){
            putchar('-');
            x = -x;
        }
        do stk[++tp]=x%10,x/=10;while(x);
        while(tp)putchar(stk[tp--]^48);
    }
}
using fastio::in;
using fastio::out;

typedef pair<int,int> PII;
const int N = 800005,C = 21;
class inquiry{
    public:
        int x,y,c;
};
vector<PII> G[N];
inquiry q[N];
int dep[N],siz[N],fa[N];
int f[N][C],g[N][2][C],sum;
int sumf[N],sumg[2][N];
int n;

void dfs1(int u,int father){
    fa[u] = father;
    dep[u] = dep[father]+1;
    siz[u] = 1;
    for(auto tmp:G[u]){
        int v = tmp.first,w = tmp.second;
        if(v==father)
            continue;
        dfs1(v,u);
        siz[u] += siz[v];
    }
}

void dfs2(int u,int father){
    f[u][20] = 1;
    for(auto tmp:G[u]){
        int v = tmp.first,w = tmp.second;
        if(v==father)
            continue;
        dfs2(v,u);
        int tmpsum = 0;
        for(int k=1;k<C;k++)
            for(int j=1;j<C;j++)
                tmpsum += f[u][k]*f[v][j]*min({k,j,w});
        cout << "H " << tmpsum << endl;
        sum += tmpsum;
        for(int k=1;k<C;k++)
            f[u][min(k,w)] += f[v][k];
    }
}

void dfs3(int u,int father){
    g[u][0][20]++;
    for(auto tmp:G[u]){
        int v = tmp.first,w = tmp.second;
        if(v==father)
            continue;
        for(int k=1;k<C;k++)
            g[u][0][min(k,w)] += f[v][k];
    }
    for(auto tmp:G[u]){
        int v = tmp.first,w = tmp.second;
        if(v==father)
            continue;
        for(int k=1;k<C;k++)
            g[u][0][min(k,w)] -= f[v][k];
        for(int k=1;k<C;k++)
            g[v][0][min(k,w)] += g[u][0][k];
        for(int k=1;k<C;k++)
            g[v][1][k] += g[u][0][k];
        for(int k=1;k<C;k++)
            g[u][0][min(k,w)] += f[v][k];
        dfs3(v,u);
    }
}

void dfs4(int u,int father){
    sumg[u][1] = sumg[father][0];
    for(auto tmp:G[u]){
        int v = tmp.first,w = tmp.second;
        if(v==father)
            continue;
        for(int k=1;k<C;k++)
            for(int j=1;j<C;j++)
                sumf[u] += f[u][k]*f[v][j]*min({k,j,w});
        dfs4(v,u);
        sumf[u] += sumf[v];
        sumg[0][u] += sumf[0][v];
    }
    sumg[1][u] = sumg[1][father]+sumg[0][father]-sumf[u];
    for(auto tmp:G[father]){
        int v = tmp.first,w = tmp.second;
        for(int k=1;k<C;k++){
            for(int j=1;j<C;j++){
                
            }
        }
    }
}

int main(){
    freopen("tatooine.in","r",stdin);
    freopen("tatooine.out","w",stdout);
    n = in();
    for(int k=1;k<n;k++){
        q[k].x = in(),q[k].y = in(),q[k].c = in();
        G[q[k].x].push_back({q[k].y,q[k].c});
        G[q[k].y].push_back({q[k].x,q[k].c});
    }
    dfs1(1,0);
    dfs2(1,0);
    dfs3(1,0);
    dfs4(1,0);
    cout << sum << endl;
    for(int k=1;k<=n;k++){
        cout << k << " : " << sumf[k] << " " << sumg[1][k]-20 << endl;
    }
    puts("------");
    for(int k=1;k<n;k++){
        int a = q[k].x,b = q[k].y;
        if(dep[a]>dep[b])
            swap(a,b);
        out(sumg[a]+sumf[b]);
        putchar('\n');
    }
    return 0;
}
